/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import top.stime21.myneighbours.R

import java.lang.ref.WeakReference;



/**
 * Created by moment on 2018/2/6.
 */

class ImageLoader {

    open fun load(url: String, image: ImageView?) {
        if (image == null) return
        var requestOptions = RequestOptions().centerCrop()
                .placeholder(R.drawable.drawable_image_default)
                .error(R.drawable.drawable_image_default)
                .transform(CenterCrop())
                .format(DecodeFormat.PREFER_RGB_565)
                .priority(Priority.LOW)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

        Glide.with(image.context)
                .load(url)
                .apply(requestOptions)
                .into(object : DrawableImageViewTarget(image) {
                })
    }

    open fun load(url: String, image: ImageView?, width: Int, height: Int) {
        if (image == null) return
        var lp = image.layoutParams
        lp.width = width
        lp.height = height
        image.layoutParams = lp
        var requestOptions = RequestOptions().centerCrop()
                .placeholder(R.drawable.drawable_image_default)
                .override(width, height)
                .format(DecodeFormat.PREFER_RGB_565)
                .error(R.drawable.drawable_image_default)
                .transform(CenterCrop())
                .dontAnimate()
                .priority(Priority.LOW)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

        Glide.with(image.context)
                .load(url)
                .apply(requestOptions)
                .into(object : DrawableImageViewTarget(image) {
                })
    }

    open fun loadCircle(url: String, image: ImageView?) {
        if (image == null) return
        var requestOptions = RequestOptions().centerCrop()
                .placeholder(R.drawable.drawable_image_default)
                .error(R.drawable.drawable_image_default)
                .format(DecodeFormat.PREFER_RGB_565)
                .transform(CenterCrop())
                .dontAnimate()
                .priority(Priority.LOW)
                .transform(CircleCrop())
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

        Glide.with(image.context)
                .load(url)
                .apply(requestOptions)
                .into(object : DrawableImageViewTarget(image) {
                })
    }

    open fun load(url: String, image: ImageView?, width: Int, height: Int, round: Int) {
        if (image == null) return
        var lp = image.layoutParams
        lp.width = width
        lp.height = height
        image.layoutParams = lp
        var requestOptions = RequestOptions().centerCrop()
                .placeholder(R.drawable.drawable_image_default)
                .error(R.drawable.drawable_image_default)
                .format(DecodeFormat.PREFER_RGB_565)
                .override(width, height)
                .priority(Priority.LOW)
                .dontAnimate()
                .transforms(CenterCrop(), RoundedCorners(DensityUtil.dip2px(image.context.applicationContext, round.toFloat())))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

        Glide.with(image.context)
                .load(url)
                .apply(requestOptions)
                .into(object : DrawableImageViewTarget(image) {
                })
    }

    open fun loadRound(url: String, image: ImageView?, round: Int) {
        if (image == null) return
        var requestOptions = RequestOptions().centerCrop()
                .placeholder(R.drawable.drawable_image_default)
                .error(R.drawable.drawable_image_default)
                .format(DecodeFormat.PREFER_RGB_565)
                .priority(Priority.LOW)
                .dontAnimate()
                .transforms(CenterCrop(), RoundedCorners(DensityUtil.dip2px(image.context.applicationContext, round.toFloat())))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

        Glide.with(image.context)
                .load(url)
                .apply(requestOptions)
                .into(object : DrawableImageViewTarget(image) {
                })
    }

    open fun clearCache(context: WeakReference<Context>) {
        Thread(Runnable {
            Glide.get(context.get()!!.applicationContext).clearDiskCache()
        }).start()
        Glide.get(context.get()!!.applicationContext).clearMemory()
    }

}